package com.konsung.ksdc.common.mqtt.parser;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.Where;
import com.konsung.ksdc.bean.MqttMsgBean;
import com.konsung.ksdc.bean.MqttMsgNotice;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.database.DatabaseHelper;
import com.konsung.ksdc.common.utils.BroadCastActions;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.config.NetConfig;

import java.sql.SQLException;
import java.util.Arrays;

/**
 * 图片消息处理器
 * Created by andy on 2017/4/19.
 */
public class DeleteContactMessageHandler extends MqttMessageHandler {

    @Override
    public byte[] parseMessage(MqttMsgBean bean) throws Exception {
        return (getMessageHeader(bean) + bean.getMsgContent()).getBytes();
    }

    @Override
    public MqttMsgBean parseMessage(String topicName, byte[] bytesContent) throws Exception {
        MqttMsgBean bean = fillMsgBeanHeader(topicName, bytesContent);
        bean.setMsgContent(buildStringContent(bytesContent));
        return bean;
    }

    /**
     * 构建文本消息内容
     * @param content 内容
     * @return 消息内容
     */
    public static String buildStringContent(byte[] content) {
        return new String(Arrays.copyOfRange(content, MSG_CONTENT_START, content.length));
    }


    @Override
    public void writeNotice(String topicName, String isRead, MqttMsgBean msg) throws Exception {
        throw new Exception("illegal operation!!!!");
    }

    @Override
    public void hanlderReceivedMessage(String topicName, byte[] datas) throws Exception {
        MqttMsgBean msg = this.parseMessage(topicName, datas);

        removeLocal(msg.getContact(), msg.getMsgContent());

        sendBroadcast(msg, BroadCastActions.ACTION_DELETE_CONTACT);
    }

    /**
     * 清除本地数据
     * @param contactUserId 联系人id
     * @param contactTopicName 名称
     * @throws SQLException 数据库异常
     */
    private void removeLocal(String contactUserId, String contactTopicName) throws SQLException {
        removeContact(contactUserId);
        removeNotice(contactUserId);
        removeMsg(contactUserId, contactTopicName);
    }

    /**
     * 删除聊天记录
     * @param contactUserId 联系人id
     * @param contactTopicName 名称
     * @throws SQLException 数据库异常
     */
    private void removeMsg(String contactUserId, String contactTopicName) throws SQLException {
        RuntimeExceptionDao dao = DatabaseHelper.getHelper(MyApplication.getContext())
                .getRuntimeExceptionDao(MqttMsgBean.class);

        DeleteBuilder db = dao.deleteBuilder();
        Where where = db.where();
        Where exp1 = where.eq("topicName", contactTopicName);
        Where exp2 = where.eq("topicName", NetConfig.currentUser
                .getUserName()).and().eq("contact", contactUserId);
        Where expOr = where.or(exp1, exp2);
        where.and(where.eq("ownerUserId",
                NetConfig.currentUser.getUserId()), expOr);

        dao.delete(db.prepare());
    }

    /**
     * 删除提醒
     * @param contactUserId 联系人id
     * @throws SQLException 数据库异常
     */
    private void removeNotice(String contactUserId) throws SQLException {
        RuntimeExceptionDao dao = DatabaseHelper.getHelper(MyApplication.getContext())
                .getRuntimeExceptionDao(MqttMsgNotice.class);
        DeleteBuilder db = dao.deleteBuilder();
        db.where().eq("ownerUserId", NetConfig.currentUser.getUserId())
                .and()
                .eq("topicName", contactUserId);
        dao.delete(db.prepare());
    }

    /**
     * 删除联系人
     * @param contactUserId 联系人id
     * @throws SQLException SQLException
     */
    private void removeContact(String contactUserId) throws SQLException {
        RuntimeExceptionDao dao = DatabaseHelper.getHelper(MyApplication.getContext())
                .getRuntimeExceptionDao(RegisterUserNetBean.class);
        DeleteBuilder db = dao.deleteBuilder();
        db.where().eq("ownerId", NetConfig.currentUser.getUserId())
                .and()
                .eq("userId", contactUserId);
        dao.delete(db.prepare());
    }

}
